package com.common.tool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 队列
 * @param <T>
 */

@SuppressWarnings({ "rawtypes", "unused","unchecked" })
public class WorkBlockingQueue<T> {
	private int defaultSize = 1000;
	private String name = "";

	private BlockingQueue blockingQueue = null;
	private boolean isStop = false;
	private Lock lock = new ReentrantLock();
	private long timeout = 86400000L;

	public WorkBlockingQueue(String name, int size) {
		if (size > 0) {
			this.defaultSize = size;
		}
		initBlockingQueue(name);
	}

	public WorkBlockingQueue(String name) {
		initBlockingQueue(name);
	}

	private void initBlockingQueue(String name) {
		this.name = name;
		this.blockingQueue = new ArrayBlockingQueue(this.defaultSize);
	}

	public void put(T t) throws InterruptedException {
		if (t == null) {
			return;
		}
		if (this.isStop) {
			new InterruptedException();
		}
		this.blockingQueue.put(t);
	}

	public Object take() throws InterruptedException {
		if ((this.isStop) && (this.blockingQueue.isEmpty())) {
			return null;
		}
		return this.blockingQueue.take();
	}

	public List<T> takeList(int size) throws InterruptedException {
		this.lock.lock();
		try {
			if ((this.isStop) && (this.blockingQueue.isEmpty())) {
				return null;
			}
			List list = new ArrayList();
			this.blockingQueue.drainTo(list, size);
			if (list.size() == 0) {
				Object t = take();
				if ((t == null) || (t.toString().equals(""))) {
					return null;
				}

				list.add(t);
			} else {
				Object temp = list.get(list.size() - 1);
				if ((temp == null) || (temp.toString().equals(""))) {
					list.remove(list.size() - 1);
				}
			}
			return list;
		} catch (Exception localException) {
		} finally {
			this.lock.unlock();
		}

		return null;
	}

	public List<T> takeListFull(int size) throws InterruptedException {
		List list = takeList(size);
		if (list == null)
			return null;
		do {
			List temp = takeList(size - list.size());
			if (temp == null) {
				break;
			}
			list.addAll(temp);
		} while (list.size() < size);

		return list;
	}

	public List<T> takeList(int size, int timeout) throws InterruptedException {
		List list = new ArrayList();
		while (list.size() < size) {
			Object t = this.blockingQueue.poll(timeout, TimeUnit.MILLISECONDS);
			if (t != null) {
				list.add(t);
			} else {
				if (list.size() != 0)
					break;
				t = take();
			}

		}

		return list;
	}

	public int getSize() {
		return this.blockingQueue.size();
	}

	public boolean isStop() {
		return this.isStop;
	}

	public synchronized void stopQueue() {
		if (this.isStop) {
			try {
				this.blockingQueue.notifyAll();
			} catch (Exception localException) {
			}
			return;
		}
		this.isStop = true;
		try {
			this.blockingQueue.put("");
		} catch (InterruptedException localInterruptedException) {
		}
		try {
			this.blockingQueue.notifyAll();
		} catch (Exception localException1) {
		}
	}

	public void startQueue() {
		this.isStop = false;
		try {
			this.blockingQueue.notifyAll();
		} catch (Exception localException) {
		}
	}

	public boolean isEmpty() {
		try {
			return this.blockingQueue.isEmpty();
		} catch (Exception e) {
		}
		return true;
	}

	public void notifyQueueAll() {
		try {
			this.blockingQueue.notifyAll();
		} catch (Exception localException) {
		}
		super.notifyAll();
	}
}
